package com.itheima.Service.ServiceImpl;

import com.itheima.Context.BaseContext;
import com.itheima.DTO.OrderDTO;
import com.itheima.Dao.OrderMapper;
import com.itheima.Service.OrderService;
import com.itheima.VO.OrderVO;
import com.itheima.common.utils.DateUtils;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.Date;

@Service
@Slf4j
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public OrderVO getOrderById(int orderId){
        OrderVO orderVO = orderMapper.findOrderById(orderId);
        return orderVO;
    }

    @Override
    @Transactional
    public Integer submitOrder(OrderDTO orderDTO) throws Exception{
        String trueCode = (String) redisTemplate.opsForHash().get("验证码", orderDTO.getTelephone());
        log.info("验证码：{}",trueCode);
        String validateCode = orderDTO.getValidateCode();
        if (!validateCode.equals(trueCode)){
            throw new RuntimeException("验证码错误");
        }



        Order order = new Order();
        try {
            String time = DateUtils.parseDate2String(orderDTO.getOrderDate(), "yyyy-MM-dd");
            order.setOrderDate(DateUtils.parseString2Date(time));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        order.setOrderStatus(Order.ORDERSTATUS_NO);
        order.setOrderType(Order.ORDERTYPE_WEIXIN);
        order.setSetmealId(orderDTO.getSetmealId());


        OrderSetting orderSetting = null;
        try {
            orderSetting = orderMapper.findByOrderDate(DateUtils.parseDate2String(order.getOrderDate()));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        if (orderSetting == null){
            throw new RuntimeException("预约日期不存在");
        }
        if (orderSetting.getNumber()==0){
            throw new RuntimeException("预约人数已满");
        }

        Order t = orderMapper.selectOrderByIdCartAndOrderDate(orderDTO.getIdCard(),order.getOrderDate());
        if (t != null){
            throw new RuntimeException("今天不可重复预约");
        }



        // member表逻辑
        Member member1 = orderMapper.selectMemberByIdCard(orderDTO.getIdCard());
        if (member1 == null){
            Member member = new Member();
            member.setName(orderDTO.getName());
            member.setIdCard(orderDTO.getIdCard());
            member.setPhoneNumber(orderDTO.getTelephone());
            member.setRegTime(DateUtils.parseDate2String(DateUtils.getToday()));

            orderMapper.insertMember(member);

            //order_setting表逻辑
            orderMapper.updateOrderSettingNumberByOrderDate(DateUtils.parseDate2String(orderDTO.getOrderDate(), "yyyy-MM-dd"));
            orderMapper.updateOrderSettingReservationsByOrderDate(DateUtils.parseDate2String(orderDTO.getOrderDate(), "yyyy-MM-dd"));


            //order表逻辑
            order.setMemberId(member.getId());
            try {
                String time = DateUtils.parseDate2String(orderDTO.getOrderDate(), "yyyy-MM-dd");
                orderMapper.insertOrder(order,time);
                return order.getId();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }



        //order_setting表逻辑
        orderMapper.updateOrderSettingNumberByOrderDate(DateUtils.parseDate2String(orderDTO.getOrderDate(), "yyyy-MM-dd"));
        orderMapper.updateOrderSettingReservationsByOrderDate(DateUtils.parseDate2String(orderDTO.getOrderDate(), "yyyy-MM-dd"));


        //order表逻辑
        order.setMemberId(member1.getId());
        try {
            String time = DateUtils.parseDate2String(orderDTO.getOrderDate(), "yyyy-MM-dd");
            orderMapper.insertOrder(order,time);
            return order.getId();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }


    }
}
